package com.indexingsystem.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.*;
import com.indexingsystem.boss.admin.mapper.IdxProductsMapper;
import com.indexingsystem.boss.admin.mapper.IdxTaskMapper;
import com.indexingsystem.boss.admin.mapper.IdxTaskProductMapper;
import com.indexingsystem.boss.common.BeanUtil;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.IdxProducts;
import com.indexingsystem.boss.entity.IdxTask;
import com.indexingsystem.boss.entity.IdxTaskWithBLOBs;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.ArrayUtil;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.service.IdxTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
  * @Discription task service
  * @Author mineary
  * @Date 14:39 2018/10/31 
  * @Param 
  * @Return 
  */
@Service
public class IdxTaskServiceImpl implements IdxTaskService {
    @Resource
    private IdxTaskMapper idxTaskMapper;
    @Resource
    private IdxProductsMapper idxProductsMapper;
    @Resource
    private IdxTaskProductMapper idxTaskProductMapper;

    @Override
    public int deleteByPrimaryKey(Integer taskId) {
        // TODO Auto-generated method stub
        return idxTaskMapper.deleteByPrimaryKey(taskId);
    }

    @Override
    public int insert(IdxTaskWithBLOBs record) {
        // TODO Auto-generated method stub
        return idxTaskMapper.insert(record);
    }

    @Override
    public int insertSelective(IdxTaskWithBLOBs record) {
        // TODO Auto-generated method stub
        return idxTaskMapper.insertSelective(record);
    }

    @Override
    public IdxTaskWithBLOBs selectByPrimaryKey(Integer taskId) {
        // TODO Auto-generated method stub
        return idxTaskMapper.selectByPrimaryKey(taskId);
    }

    @Override
    public int updateByPrimaryKeySelective(IdxTaskWithBLOBs record) {
        // TODO Auto-generated method stub
        return idxTaskMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(IdxTaskWithBLOBs record) {
        // TODO Auto-generated method stub
        return idxTaskMapper.updateByPrimaryKeySelective(record);
    }


    @Override
    public int updateByPrimaryKey(IdxTask record) {
        // TODO Auto-generated method stub
        return idxTaskMapper.updateByPrimaryKey(record);
    }
    //1
    @Override
    public List<IdxTaskWithBLOBs> selectTaskByParentId(Integer parentId) {
        List<Integer> parentIdList = new ArrayList<Integer>();
        List<IdxTaskWithBLOBs> parentList = new ArrayList<IdxTaskWithBLOBs>();
        parentIdList.add(parentId);
        List<IdxTaskWithBLOBs> list = selectAllParentIdByParentId(parentList, parentIdList);
        return list;
    }

    @Override
    public Integer updateTaskArrchildIdByParentId(Integer taskId, Integer parentId) {
        Integer value = null;
        StringBuffer sb = null;
        List<IdxTaskWithBLOBs> parentList = new ArrayList<IdxTaskWithBLOBs>();
        List<Integer> parentIdList = new ArrayList<Integer>();
        parentIdList.add(parentId);
        List<IdxTaskWithBLOBs> list = selectAllParentIdByParentId(parentList, parentIdList);
        if (null != list && list.size() > 0) {
            for (IdxTaskWithBLOBs idxTaskWithBLOBs : list) {
                sb = new StringBuffer();
                if (StringUtils.isNotBlank(idxTaskWithBLOBs.getArrchildid()) && !idxTaskWithBLOBs.getArrchildid().equals("-1")) {
                    sb.append(idxTaskWithBLOBs.getArrchildid()).append(",").append(taskId);
                } else {
                    sb.append(taskId);
                }
                idxTaskWithBLOBs.setChild(1);
                idxTaskWithBLOBs.setArrchildid(sb.toString());
                value = idxTaskMapper.updateByPrimaryKeySelective(idxTaskWithBLOBs);
            }
        }
        return value;
    }

    private List<IdxTaskWithBLOBs> selectAllParentIdByParentId(List<IdxTaskWithBLOBs> parentList, List<Integer> parentIdList) {

        List<IdxTaskWithBLOBs> idxTaskWithBLOBsList = null;
        List<Integer> list = null;
        if (null != parentIdList && parentIdList.size() > 0) {
            idxTaskWithBLOBsList = idxTaskMapper.selectByParentId(parentIdList);
            if (null != idxTaskWithBLOBsList && idxTaskWithBLOBsList.size() > 0) {
                for (IdxTaskWithBLOBs idxTaskWithBLOBs : idxTaskWithBLOBsList) {
                    list = new ArrayList<Integer>();
                    parentList.add(idxTaskWithBLOBs);
                    if (null != idxTaskWithBLOBs.getParentid() && idxTaskWithBLOBs.getParentid() != -1) {
                        list.add(idxTaskWithBLOBs.getParentid());
                    } else {
                        list.add(null);
                    }
                }
            }
            selectAllParentIdByParentId(parentList, list);
        }
        return parentList;
    }

    @Override
    public PagedResult<TaskProductListVo> getTasksList(TaskProductListReqVo taskProductListReqVo) {
        Integer pageNo = taskProductListReqVo.getPageNo() == null ? 1 : taskProductListReqVo.getPageNo();
        Integer pageSize = taskProductListReqVo.getPageSize() == null ? 10 : taskProductListReqVo.getPageSize();
        // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtils.toPagedResult(idxTaskMapper.getTasksList(taskProductListReqVo));
    }

    @Override
    public Integer getTasksListCount(TaskProductListReqVo taskProductListReqVo) {
        // TODO Auto-generated method stub
        return idxTaskMapper.getTasksListCount(taskProductListReqVo);
    }

    public List<IdxTaskWithBLOBs> getTaskById(Integer taskId) {
        // TODO Auto-generated method stub
        return idxTaskMapper.getTaskById(taskId);
    }

    @Override
    public Integer getParentIdByTaskId(Integer taskId) {
        return idxTaskMapper.getParentIdByTaskId(taskId);
    }

    @Override
    public Integer deleteByIdBatch(List<String> list) {
        // TODO Auto-generated method stub
        return idxTaskMapper.deleteByIdBatch(list);
    }

    @Override
    public Integer updateTaskById(List<String> list) {
        // TODO Auto-generated method stub
        Integer value = 0;
        List<IdxTaskWithBLOBs> idxTaskWithBLOBsList = idxTaskMapper.selectAllList();
        if (null != idxTaskWithBLOBsList && idxTaskWithBLOBsList.size() > 0) {
            for (String taskId : list) {
                for (IdxTaskWithBLOBs idxTaskWithBLOBs : idxTaskWithBLOBsList) {
                    if (StringUtils.isNotBlank(idxTaskWithBLOBs.getArrchildid())) {
                        idxTaskWithBLOBs.setArrchildid(idxTaskWithBLOBs.getArrchildid().replace("," + taskId, "").replace(taskId + ",", "").replace(taskId, ""));
                    }
                    value = idxTaskMapper.updateByPrimaryKeySelective(idxTaskWithBLOBs);
                }
            }
        }
        return value;

    }

    @Override
    public PagedResult<IdxTaskWithBLOBs> getUnFinishedTaskList(TaskListReqVo reqVo) {
        Integer pageNo = reqVo.getPageNo() == null ? 1 : reqVo.getPageNo();
        Integer pageSize = reqVo.getPageSize() == null ? 20 : reqVo.getPageSize();

        // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.toPagedResult(idxTaskMapper.getUnFinishedTaskList(reqVo));
    }

    @Override
    public List<String> getArrchildidsByTaskId(Integer taskId) {
        String childIds = idxTaskMapper.getArrchildidsByTaskId(taskId);
        if (childIds != null && childIds != "") {
            List<String> idList = ArrayUtil.toArrayList(childIds.split(","));
            return idList;
        } else {
            return null;
        }
    }

    @Override
    public Integer updateCompleteStatusByIdBatch(Integer status, List<String> idList) {
        return idxTaskMapper.updateCompleteStatusByIdBatch(status, idList);
    }

    @Override
    public PagedResult<IdxProducts> getUnassignProducts(TaskProductAddReqVo reqVo) {
        Integer pageNo = reqVo.getPageNo() == null ? 1 : reqVo.getPageNo();
        Integer pageSize = reqVo.getPageSize() == null ? 20 : reqVo.getPageSize();
        //获取未完成的任务
        List<String> tasks = idxTaskMapper.getUnCompleteTask();
        tasks.add(String.valueOf(reqVo.getTaskId()));
        List<String> taskList = new ArrayList<>();
        List<String> productIds = null;
        if (tasks != null && tasks.size() > 0) {
            taskList.addAll(tasks);
            for (String str : tasks) {
                List<String> ids = null;
                if (StringUtils.isNotBlank(str)) {
                    //获取主任务的子任务
                    ids = getArrchildidsByTaskId(Integer.valueOf(str));
                }
                if (ids!= null && ids.size() > 0)
                    taskList.addAll(ids);
            }
        }
        if (taskList != null && taskList.size() > 0) {
            //列表去重。
            taskList = mixedList(taskList);
            LogCvt.info(taskList.toString());
        }
        if (taskList != null && taskList.size() > 0) {
            productIds = idxTaskProductMapper.getProductIdByTaskId(taskList);
        }
        // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。

        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.toPagedResult(idxProductsMapper.getUnAssignProductList(productIds,reqVo));
    }

    @Override
    public PagedResult<TaskProductListResVo> getCheckTasksList(TaskProductListReqVo reqVo) {
        Integer pageNo = reqVo.getPageNo() == null ? 1 : reqVo.getPageNo();
        Integer pageSize = reqVo.getPageSize() == null ? 20 : reqVo.getPageSize();

        // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        PageHelper.startPage(pageNo, pageSize);
        return BeanUtil.toPagedResult(idxTaskMapper.getCheckTasksList(reqVo));
    }

    @Override
    public Integer selectCountByTaskIdAndStatus(TaskProductAddReqVo reqVo) {
        return idxTaskMapper.selectCountByTaskIdAndStatus(reqVo);
    }



    @Override
    public List<String> mixedList(List<String> list1) {
        Map<String, String> map = new HashMap<>();
        for (String str : list1) {
            if (str != null) {
                map.put(str, str);
            }
        }
        List<String> combineResult = new ArrayList<>();
        Set<Map.Entry<String, String>> entryset = map.entrySet();
        Iterator<Map.Entry<String, String>> iter = entryset.iterator();
        while (iter.hasNext()) {
            combineResult.add(iter.next().getValue());
        }
        return combineResult;

    }

    /**
     *
     * @return
     */
    @Override
    public List<String> getTopTaskIds() {
        return idxTaskMapper.getTopTaskIds();
    }


}
